package com.dcy.znoa.service.estate.impl;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.DateUtil;
import com.dcy.znoa.common.util.Dysms;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.estate.model.notice.SelectEstateNoticeModel;
import com.dcy.znoa.controller.publiz.model.messNotice.AddMessNoticeListAndMsgModel;
import com.dcy.znoa.controller.sys.model.storeFile.AddStoreFileModel;
import com.dcy.znoa.dao.estate.EstateNoticeCompMapper;
import com.dcy.znoa.dao.estate.EstateNoticeMapper;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.estate.EstateNotice;
import com.dcy.znoa.domain.estate.EstateNoticeComp;
import com.dcy.znoa.domain.publiz.MessNotice;
import com.dcy.znoa.service.estate.EstateNoticeService;
import com.dcy.znoa.service.publiz.MessNoticeService;
import com.dcy.znoa.service.sys.StoreFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;
import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 物业通知： 服务实现类
 * </p>
 *
 * @author cook
 * @since 2018-05-21
 */
@Slf4j
@Service
public class EstateNoticeServiceImpl extends ServiceImpl<EstateNoticeMapper, EstateNotice> implements EstateNoticeService {

    @Autowired
    EstateNoticeMapper enMapper;
    @Autowired
    EstateNoticeCompMapper encMapper;
    @Autowired
    OperDao operDao;
    @Autowired
    MessNoticeService messNoticeService;
    @Autowired
    StoreFileService storeFileService;

    @Override
    public JsonForList selectList(SelectEstateNoticeModel model) throws InvocationTargetException, IllegalAccessException {
        EstateNotice cp = new EstateNotice();
        BeanUtils.copyProperties(cp, model);
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List<EstateNotice> list = enMapper.selectListMapper(model, page);
        return new JsonForList(list, page.getTotal());
    }

    @Transactional
    @Override
    public boolean add(EstateNotice cp) throws BusinessException, IllegalAccessException, ClientException, InvocationTargetException {
        // 1 新增主表
        Integer result1 = enMapper.insert(cp);
        if(result1==null||result1<=0){
           throw new BusinessException(INSERT_ERROR_CODE,INSERT_ERROR_MSG);
        }
        String compIds = cp.getCompIds();
        String[] str = compIds.split(",");
        //2  发消息
        List<Oper> operList = operDao.selectList(new EntityWrapper<Oper>().in("comp_id",str));
        List<Long> operIdList = operList.stream().map(Oper::getOperId).collect(toList());
        Long[] operIdsStr = operIdList.toArray(new Long[operList.size()]);
        AddMessNoticeListAndMsgModel amnlModel = new AddMessNoticeListAndMsgModel();
        BeanUtils.copyProperties(amnlModel, cp);
//        amnlModel.setMessage(cp.getMessage());
        amnlModel.setMessTitle("物业通知:"+DateUtil.getFormatNowDate(DateUtil.datetimeFormat));
//        amnlModel.setCreateOperId(cp.getCreateOperId());
        amnlModel.setOperIdsStr(DateUtil.toString(operIdsStr));
        Long  esonId = cp.getEsnoId();
        log.info("esonId:"+ esonId);
        amnlModel.setEntityId(esonId);
        amnlModel.setEntityTbl("estate_notice");
        amnlModel.setMessType(5);//5表示物业通知
        amnlModel.setIsSms(cp.getIsSms());

        //给接收人的消息
        Long messId = messNoticeService.insertBatchAndMsg(amnlModel);
        if(messId==null||messId<0){
            throw new BusinessException("404", "新增物业通知-内部消息发送失败");
        }
        //3 新增对应关系表
        int length  = str.length;
        List<Long> compIdList = new ArrayList<>(length);
        for(int i=0;i<length;i++){
            compIdList.add(Long.valueOf(str[i]));
        }
        int result2 = encMapper.addBatch(cp.getEsnoId(),compIdList);
        if(result2<=0){
            throw new BusinessException(INSERT_ERROR_CODE,INSERT_ERROR_MSG);
        }
//        //4 操作附件表
//        if(StringUtils.isNotBlank(cp.getRemoteFileName())){
//            AddStoreFileModel addStoreFileModel = new AddStoreFileModel();
//            BeanUtils.copyProperties(addStoreFileModel, cp);
//            addStoreFileModel.setEntityTbl("estate_notice");
//            addStoreFileModel.setEntityId(cp.getEsnoId());
//            Integer result3 = storeFileService.addBatch(addStoreFileModel);
//            if (result3 == null || result2 <= 0) {
//                throw new BusinessException("404", "新增物业服务单附件失败");
//            }
//        }
        return true;
    }
    @Transactional
    @Override
    public boolean delById(Long esnoId) throws BusinessException {
        boolean result1 = deleteById(esnoId);
        if(!result1){
            throw new BusinessException(DELETE_ERROR_CODE,DELETE_ERROR_MSG);
        }
        Integer result2 =encMapper.delete(new EntityWrapper<EstateNoticeComp>().eq(("esno_id"),esnoId));
        if(result2==null || result2 <=0){
            throw new BusinessException(DELETE_ERROR_CODE,DELETE_ERROR_MSG);
        }
        return true;
    }

}
